<?php
/**
*
* 功能描述（用户数据）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class ModelUser extends ModelBase
{
	function CreateUser($type, $mobile, $pwd)
	{
		$data = array('type' => $type, 'mobile' => $mobile, 'password' => md5($pwd), 'name' => $mobile, 'email' => $mobile . " not set");
		$id = $this->db->Insert("user", $data);
		return $id;
	}

	function GetUserList($page)
	{
		$data  = $this->db->getRow("user", 'count(1) cnt', "1");
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());

		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / ADM_PAGESIZE));
		$limit = ADM_PAGESIZE * ($page - 1) . ", " . ADM_PAGESIZE;
		$data = $this->db->getAll("user", "id, `type`, level, name, mobile, email, score, money, truename, sex, regtime, lastlogin, com_name", "1", 'level desc', $limit);
		$retData['data'] = $data;
		return $retData;
	}

	function SetLevel($type, $id, $level)
	{
		$data = array(
			'level' => $level,
		);
		$cnt = $this->db->Update("user", $data, "`id` = $id and `type` = $type");
		return $cnt;
	}

	function SetPwd($id, $pwd)
	{
		$where = "`id` = $id";
		$data = array(
			'password' => md5($pwd),
		);
		$cnt = $this->db->Update("user", $data, $where);
		return $cnt;
	}

	function GetBaseInfo($userid)
	{
		$fild = "`type`, `mobile`, `name`, `email`, `truename`, `level`, `com_name`, `com_tel`";
		$data = $this->db->GetRow("user", $fild, "id = $userid");
		return $data;
	}

	function GetInfo($userid)
	{
		$fild = "`id`, `type`, `mobile`, `name`, `email`, `truename`, `bYear`, `bMonth`, `bDay`, `sex`, `level`, `key` != 0 lostpwd, `score`, `money`, `regtime`, `lastlogin`, com_name, com_tel";
		$data = $this->db->GetRow("user", $fild, "id = $userid");
		return $data;
	}

	function GetUser($mobile)
	{
		$fild = "`id`, `type`, `mobile`";
		$data = $this->db->GetRow("user", $fild, "mobile = $mobile");
		return $data;
	}

	function AddAmount($id, $amount)
	{
		$sql = "update user set money = money + $amount where id = $id";
		$cnt = $this->db->execute($sql);
		return $cnt;
	}

	function GetAccInfo($id)
	{
		$data  = $this->db->getRow("user", 'mobile, email, round(money, 3) money, score, regtime, lastlogin, com_name, com_tel, type, level', "id = $id");
		return $data;
	}

	function DecMoney($uid, $money)		//余额
	{
		$sql = "update user set money = money - $money where id = $uid && money >= $money";
		return $this->db->execute($sql);
	}

	function IncScore($uid, $score)		//加积分
	{
		$sql = "update user set score = score + $score where id = $uid";
		return $this->db->execute($sql);
	}
}

